package com.zjj.algorithm.learning.atguigu.tree;

/**
 * 将数组使用二叉树存储【完全二叉树】
 *
 * @author zjj_admin
 * @date 2022/11/24 22:22
 */
public class ArrBinaryTreeDemo {

    public static void main(String[] args) {
        int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
        ArrBinaryTree arrBinaryTree = new ArrBinaryTree(arr);
        arrBinaryTree.preOrder();
    }
}


class ArrBinaryTree {

    private int[] arr;

    public ArrBinaryTree(int[] arr) {
        this.arr = arr;
    }

    public void preOrder(){
        this.preOrder(0);
    }

    private void preOrder(int index){
        if(this.arr == null || this.arr.length == 0){
            System.out.println("数组中没有数据");
        }
        System.out.println(arr[index]);

        if(index * 2 + 1 < arr.length){
            preOrder(index * 2 + 1);
        }
        if(index * 2 + 2 < arr.length){
            preOrder(index * 2 + 2);
        }
    }
}